<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/09 14:32
 * Blog：www.myblogs.xyz
 */

namespace app\common\model;


class Messages extends Common
{
    protected $name = 'messages';
    protected $pk = 'messages_id';

    public function messagesLists($where = '', $limit = 1, $order = '', $field = '', $whereOr = '')
    {
        $data = $this->getLists($where, $limit, $order, $field, $whereOr);
        $list = $data['list'];
        if (is_array($list)){
            $member_model = new Member();
            $admin_model = new Admin();
            foreach ($list as $key=>$val){
                $list[$key]['target_name'] = '全部';
                if (!empty($val['target_id'])){
                    if ($val['target'] == 1){
                        $list[$key]['target_name'] = $member_model->getField($val['target_id'], 'mobile');
                    }elseif ($val['target'] == 2){
                        $list[$key]['target_name'] = $admin_model->getField($val['target_id'], 'nickname');
                    }
                }
            }
        }
        $data['list'] = $list;
        return $data;
    }

    public function getMessagesId($target, $target_id, $is_read = false)
    {
        if ($target == 2){
            // get admin register time
            $admin_model = new Admin();
            $time = $admin_model->getField($target_id, 'create_time');
        }else{
            // get member register time
            $member_model = new Member();
            $time = $member_model->getField($target_id, 'create_time');
        }
        // 获取系统信息
        $messages_id = [];
        $list_where['send_time'] = ['egt', $time];
        $list_where['target'] = $target;
        $list_where['target_id'] = 0;
        $read_messages = new ReadMessages();
        $messages_fields = 'messages_id,title,create_time,target_id';
        $messages_list = $this->getList($list_where, '', '', $messages_fields);
        if (!empty($messages_list)){
            if (is_array($messages_list)){
                foreach ($messages_list as $key=>$val){
                    if ($is_read){
                        if ($read_messages->isRead($target, $target_id, $val['messages_id'])){
                            continue;
                        }
                    }
                    if (!$read_messages->isDel($target, $target_id, $val['messages_id'])){
                        $messages_id[] = $val['messages_id'];
                    }
                }
            }
        }
        $list_where['target_id'] = $target_id;
        $messages_ids = $this->getList($list_where, '', '', 'messages_id');
        $array_id = [];
        if (!empty($messages_ids)){
            if (is_array($messages_ids)){
                foreach ($messages_ids as $key=>$val){
                    if ($is_read){
                        if ($read_messages->isRead($target, $target_id, $val['messages_id'])){
                            continue;
                        }else{
                            $array_id[] = $val['messages_id'];
                        }
                    }else{
                        $array_id[] = $val['messages_id'];
                    }
                }
            }
        }
        $ids_array = array_merge($messages_id, $array_id);
        return $ids_array;
    }

    /**
     * 发送系统消息
     * @param $member_id
     * @param $message_type
     * @param string $contents
     * @param bool $is_sms
     * @param bool $is_sys
     */
    public function sendMessage($member_id, $message_type, $contents = '', $is_sms = false, $is_sys = true)
    {
        $curr_time = NEW_TIME;
        // $message_type，1骑行时间过长，2停止时间过长，3
        $title = '';
        if ($message_type == 1){
            $title = '使用车辆温馨提醒';
            $contents = '您使用的车辆骑行时间过长，注意休息请勿疲劳驾驶！';
        }
        if ($message_type == 2){
            $title = '使用车辆温馨提醒';
            $contents = '您使用的车辆暂停时间过长，记得还车！';
        }
        if ($message_type == 3){
            $title = '使用车辆温馨提醒';
            if (empty($contents)){
                $basic_model = new Setting();
                $basic = $basic_model->settingFind('basic');
                if (!empty($basic['outside_time'])){
                    $contents = '您使用的车辆已在区域外停止了' . intval($basic['outside_time'])/2 . '分钟，停止时间超过' . intval($basic['outside_time']) . '分钟后将自动结束订单！';
                }
            }
        }
        if ($message_type == 4){
            $title = '使用车辆温馨提醒';
            if (empty($contents)){
                $basic_model = new Setting();
                $basic = $basic_model->settingFind('basic');
                if (!empty($basic['area_time'])){
                    $contents = '您使用的车辆已停止了' . intval($basic['area_time'])/2 . '分钟，停止时间超过' . intval($basic['area_time']) . '分钟后车自动结束订单！';
                }
            }
        }
        if ($message_type == 5){
            $title = '系统赠送';
            if (empty($contents)){
                $contents = '系统赠送了您一张优惠券';
            }
        }
        if ($message_type == 6){
            $title = '系统充值';
            if (empty($contents)){
                $contents = '系统充值成功提醒';
            }
        }
        // 发送系统消息提醒
        $message_data = [
            'title'=>$title,
            'contents'=>$contents,
            'create_time'=>$curr_time,
            'update_time'=>$curr_time,
            'send_time'=>$curr_time,
            'target_id'=>$member_id,
            'is_push'=>1,
            'is_send'=>1,
        ];
        if ($is_sys){
            $message_id = $this->insert($message_data, true);
            // 发送APP推送消息提醒
            /*$push_data = [
                'type'=>'system',
                'contents'=>$message_data['contents'],
                'ids'=>$message_id
            ];
            $member_model = new Member();
            $token = $member_model->memberField($member_id, 'token');
            if (!empty($token)){
                $setting_model = new Setting();
                $setting_model->ruleExec($message_data['title'], $token, $push_data);
            }*/
        }
        // 发送短信提醒
        if ($is_sms){

        }
    }

}